package com.huawei.hwid.core.b.a;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import com.huawei.hms.framework.network.restclient.Headers;
import com.huawei.hms.framework.network.restclient.Response;
import com.huawei.hms.framework.network.restclient.hwhttp.ResponseBody;
import com.huawei.hms.support.api.entity.sns.SNSCode.Status;
import com.huawei.hwid.core.b.a.a.a;
import com.huawei.hwid.core.b.a.a.c;
import com.huawei.hwid.core.b.a.a.f;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.h;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.exception.TokenInvalidatedException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.net.ssl.SSLPeerUnverifiedException;
import org.xmlpull.v1.XmlPullParserException;

public class e {
    private static boolean a = false;
    private static final Executor b = Executors.newFixedThreadPool(12);

    public static void a(Context context, a aVar, String str, Handler handler) {
        com.huawei.hwid.core.d.b.e.b("RequestManager", "Start sendRequestAsyn.", true);
        if (aVar.o() <= 0) {
            com.huawei.hwid.core.d.b.e.b("RequestManager", "Invalid siteID, start setGlobalSiteId.", true);
            aVar.a(context, b.a(context, str));
        }
        b.execute(new f(context, aVar, handler, str));
    }

    public static Bundle a(Context context, a aVar, String str) {
        a(context, aVar, str, 3 - aVar.q(), 0);
        Bundle e = aVar.e();
        if (200 == aVar.f() && aVar.g() == 0) {
            e.putAll(aVar.c());
        }
        return e;
    }

    private static void a(Context context, a aVar) {
        if (!(aVar instanceof c) && aVar.f() != 1007) {
            aVar.e(b.a());
            com.huawei.hwid.core.a.c.a(aVar.b(context), context);
            aVar.r();
        }
    }

    private static boolean a(Context context, a aVar, String str, int i) {
        if (i == 2) {
            com.huawei.hwid.core.d.b.e.b("RequestManager", "Will check and download global file.", true);
            b(context, aVar, str, i);
            return true;
        } else if (i < 3) {
            return true;
        } else {
            com.huawei.hwid.core.d.b.e.b("RequestManager", "exceed max request try time", true);
            return false;
        }
    }

    private static void a(Context context, a aVar, String str, int i, int i2) {
        com.huawei.hwid.core.d.b.e.b("RequestManager", "sendRequestSyn: " + aVar.getClass().getSimpleName() + " -- startFromTimes: " + i, false);
        if (a(context, aVar, str, i)) {
            int i3 = i + 1;
            int i4 = i2 + 1;
            if (i4 > 1) {
                a(context, aVar);
                i4 = 1;
            }
            com.huawei.hwid.core.d.b.e.b("RequestManager", "startFromTimes1::=" + i3, true);
            aVar.d(b.a());
            try {
                if (b(context, aVar, str, i3, i4)) {
                    return;
                }
            } catch (SSLPeerUnverifiedException e) {
                a(aVar, i4, e.getClass().getSimpleName(), (int) Status.GET_USER_UNREAD_MSG_FAIL);
            } catch (UnsupportedEncodingException e2) {
                a(aVar, i4, e2.getClass().getSimpleName(), 1001);
            } catch (IllegalArgumentException e3) {
                a(aVar, i4, e3.getClass().getSimpleName(), 1002);
            } catch (IllegalStateException e4) {
                a(aVar, i4, e4.getClass().getSimpleName(), 1003);
            } catch (IOException e5) {
                a(aVar, i2, e5.getClass().getSimpleName(), 1005);
                a(context, aVar, str, i3, i4);
            } catch (XmlPullParserException e6) {
                a(aVar, i4, e6.getClass().getSimpleName(), 1006);
            } catch (TokenInvalidatedException e7) {
                a(aVar, i4, e7.getClass().getSimpleName(), 3000, e7.getMessage());
            } catch (NullPointerException e8) {
                a(aVar, i4, e8.getClass().getSimpleName(), (int) Status.HWID_UNLOGIN, e8.getMessage());
            }
            a(context, aVar);
        }
    }

    private static boolean b(Context context, a aVar, String str, int i, int i2) throws IOException, XmlPullParserException, TokenInvalidatedException {
        if (b.a(context)) {
            Response a = c.a(context, aVar, str);
            Headers headers = a.getHeaders();
            int code = a.getCode();
            com.huawei.hwid.core.d.b.e.b("RequestManager", "httpResponseCode = " + code, false);
            String a2 = a(a);
            com.huawei.hwid.core.d.b.e.b("RequestManager", "response responseXMLContent:  " + a2, false);
            if (TextUtils.isEmpty(a2) || !a2.contains("<html")) {
                aVar.a(code);
                a(a, aVar, i2, code, context, str, i, headers, a2);
                return false;
            }
            aVar.a(1001);
            aVar.a(i2, 1001, "UNSUPPORTED_ENCODING_EXCEPTION");
            return true;
        }
        aVar.a(1007);
        return true;
    }

    private static void a(a aVar, int i, String str, int i2, String str2) {
        com.huawei.hwid.core.d.b.e.d("RequestManager", str, true);
        aVar.a(i2);
        aVar.a(i, i2, str2);
    }

    private static void a(a aVar, int i, String str, int i2) {
        com.huawei.hwid.core.d.b.e.d("RequestManager", str, true);
        aVar.a(i2);
        aVar.a(i, i2, str);
    }

    private static String a(Response<ResponseBody> response) throws IOException {
        String str = "";
        if (response == null || response.getBody() == null) {
            return str;
        }
        return new String(((ResponseBody) response.getBody()).bytes(), "UTF-8");
    }

    private static void b(Context context, a aVar, String str, int i) {
        if (b(context, aVar)) {
            com.huawei.hwid.core.d.b.e.b("RequestManager", "Download global from sendRequestSyn.", true);
            try {
                if (h.b(context, null)) {
                    com.huawei.hwid.core.d.b.e.b("RequestManager", "Start to request again after refresh global.", true);
                    aVar.a(context, aVar.o());
                    a(context, aVar, str, i, 0);
                }
            } catch (IOException e) {
                com.huawei.hwid.core.d.b.e.d("RequestManager", "IOException when download global.", true);
            }
        }
    }

    private static void a(Response response, a aVar, int i, int i2, Context context, String str, int i3, Headers headers, String str2) throws IOException, XmlPullParserException, TokenInvalidatedException {
        if (200 == i2) {
            if (str2 != null) {
                try {
                    com.huawei.hwid.core.d.b.e.e("RequestManager", "parse response start", true);
                    if (a.c.URLType.equals(aVar.d())) {
                        aVar.b(str2);
                    } else {
                        aVar.a(str2);
                    }
                    aVar.a(i, 200, "");
                    com.huawei.hwid.core.d.b.e.e("RequestManager", "parse response end", true);
                    b(context, aVar, headers != null ? headers.get("Set-Cookie") : "");
                    if (!(aVar instanceof f)) {
                        c(context, aVar, str, i3, i);
                    } else if (a) {
                        a(false);
                        c(context, aVar, str, i3, i);
                    }
                } catch (IOException e) {
                    throw e;
                } catch (XmlPullParserException e2) {
                    throw e2;
                } catch (TokenInvalidatedException e3) {
                    throw e3;
                }
            }
        } else if (307 == i2) {
            a(response, aVar, i, context, str, i3);
        } else {
            aVar.a(i, i2, "other code");
            a(context, aVar, str, i3, i);
        }
    }

    private static boolean b(Context context, a aVar) {
        if (!h.a(context) || (aVar instanceof a) || (aVar instanceof c)) {
            return false;
        }
        com.huawei.hwid.core.d.b.e.b("RequestManager", "should download global.", true);
        return true;
    }

    private static void a(Response response, a aVar, int i, Context context, String str, int i2) {
        aVar.a(i, 307, "SC_TEMPORARY_REDIRECT");
        Headers headers = response.getHeaders();
        String str2 = headers == null ? "" : headers.get("location");
        if (!TextUtils.isEmpty(str2)) {
            aVar.c(str2);
            a(context, aVar, str, i2, i);
        }
    }

    private static void b(Context context, a aVar, String str) {
        if (!TextUtils.isEmpty(str) && aVar.g() == 0) {
            Object obj = null;
            if (aVar instanceof f) {
                obj = ((f) aVar).s();
            }
            if (!TextUtils.isEmpty(obj)) {
                com.huawei.hwid.d.a.a(context).a(obj, str);
            }
        }
    }

    private static void c(Context context, a aVar, String str, int i, int i2) throws IOException, TokenInvalidatedException {
        if (!TextUtils.isEmpty(str)) {
            if (b(aVar)) {
                throw new TokenInvalidatedException("token is invalidated");
            } else if (a(aVar)) {
                com.huawei.hwid.core.d.b.e.b("RequestManager", "user session is out of date.", true);
                com.huawei.hwid.d.a.a(context).c();
                if (a(context, str, aVar)) {
                    a(context, aVar, str, i, i2);
                }
            }
        }
    }

    private static boolean a(a aVar) {
        if (70001101 == aVar.g()) {
            return true;
        }
        return false;
    }

    private static boolean b(a aVar) {
        if (70002015 == aVar.g() || 70002016 == aVar.g()) {
            return true;
        }
        return false;
    }

    private static boolean a(Context context, String str, a aVar) throws IOException, TokenInvalidatedException {
        HwAccount b = com.huawei.hwid.d.a.a(context).b();
        Object obj = "";
        String str2 = "";
        String str3 = "";
        String str4 = "";
        if (b != null) {
            obj = b.g();
            str2 = b.c();
            str3 = b.i();
            str4 = b.k();
        }
        if (TextUtils.isEmpty(obj)) {
            throw new TokenInvalidatedException("token is null");
        }
        a(true);
        a fVar = new f(context, str2, obj, b.a(context, str), str3, str4);
        a(context, fVar, str, 0, 0);
        aVar.a(fVar.f());
        if (fVar.f() == 200 && (fVar.g() == 70002016 || fVar.g() == 70002015)) {
            aVar.a(3000);
        }
        if (200 != aVar.c().getInt("responseCode")) {
            return false;
        }
        if (fVar.g() == 0) {
            return true;
        }
        return false;
    }

    private static synchronized void a(boolean z) {
        synchronized (e.class) {
            a = z;
        }
    }
}
